﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using AutoMapper;
using GMS.IServices;
using GMS.IServices.Jwt;
using GMS.Models.Dto;
using GMS.Models.Entity;
using GMS.Models.ResponseDto;
using GMS.Models.SearchDto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GMS.API.Controllers
{
    [Authorize]
    [ApiController]
    [Route("UserInfo")]
    public class UserInfoController : ControllerBase
    {
        private readonly IMapper _mapper;
        private readonly IUserInfoService _userInfoService;
        private readonly IUserRolesService _userRolesService;
        private readonly IJwtService _jwtService;

        public UserInfoController(IMapper mapper, IUserInfoService userInfoService, IUserRolesService userRolesService, IJwtService jwtService)
        {
            _mapper = mapper;
            _userInfoService = userInfoService;
            _userRolesService = userRolesService;
            _jwtService = jwtService;
        }


        [HttpGet("Current")]
        public async Task<IActionResult> GetUserInfo()
        {
            //模拟Token解析到当前操作的用户
            var payload = await _jwtService.ParseClaim((ClaimsIdentity)HttpContext.User.Identity);
            var entity = await _userInfoService.GetUserAsync(payload.Id);
            var responseDto = _mapper.Map<UserInfoResponseDto>(entity);
            return StatusCode(200, new { code = 200, message = "获取当前用户成功", data = responseDto });
        }

        [HttpDelete("deleted")]
        public async Task<IActionResult> Deleted(Guid id)
        {
            var res = await _userInfoService.DeleteAsync(id);
            return res > 0
                ? StatusCode(200, new { code = 200, message = "删除用户成功" })
                : BadRequest(new { code = 500, message = "删除用户失败" });
        }

        /// <summary>
        /// 删除多个用户
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete("deletedMany")]
        public async Task<IActionResult> DeletedMany(string ids)
        {
            var res = await _userInfoService.DeleteManyAsync(ids);
            return res > 0
                ? StatusCode(200, new { code = 200, message = "删除用户成功" })
                : BadRequest(new { code = 500, message = "删除用户失败" });
        }


        [HttpPut("edited")]
        public async Task<IActionResult> Edited(UserInfoDto dto)
        {
            var entity = _mapper.Map<UserInfo>(dto);
            var res = await _userInfoService.UpdatedAsync(entity);
            var status = dto.IsDel ? "禁用" : "启用";
            return res > 0
                ? StatusCode(200, new { code = 200, message = $"{status}用户成功" })
                : BadRequest(new { code = 500, message = "禁用用户失败" });
        }


        [HttpGet("All")]
        public async Task<IActionResult> GetAll([FromQuery] UserInfoSearchDto dto)
        {
            var pagedList = await _userInfoService.GetMultipleAsync(dto);
            var userRolesEntities = await _userRolesService.GetUserAllAsync(pagedList.Result.Select(x => x.Id).ToList());
            var userRolesResponseDto = _mapper.Map<List<UserRolesResponseDto>>(userRolesEntities);
            pagedList.Result.ForEach(x =>
            {
                x.UserRoles = userRolesResponseDto.Where(c => c.UserId == x.Id).ToList();
            });

            return StatusCode(200, new { code = 200, message = "获取用户成功", data = pagedList });

        }

        [HttpPost("added")]
        public async Task<IActionResult> Added(UserInfoDto dto)
        {
            var entity = _mapper.Map<UserInfo>(dto);
            var res = await _userInfoService.AddedAsync(entity);
            return res > 0
                ? StatusCode(200, new { code = 200, message = "添加用户成功" })
                : BadRequest(new { code = 500, message = "添加用户失败" });
        }


    }
}